package lmteam.documentdataservice;

import org.junit.Test;
import static org.junit.Assert.assertTrue;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertNotNull;
import static org.junit.Assert.assertNull;
import static org.junit.Assert.assertFalse;

import lmteam.common.*;


public class DocumentDataProcessorTest
{
    private static User tomuser = new User("tom","tom", new Role(true, true, true));
    private static User laurauser = new User("laura","laura", new Role(true, true, true));
    private static User blakeuser = new User("blake","blake", new Role(true, true, true));
    private static User johnuser = new User("john","john", new Role(true, true, true));


    @Test
    public void testAddandDelete()
	throws java.rmi.RemoteException
    {
	DocumentDataProcessor ddp = new DocumentDataProcessor();
	Document document = new Document();

	document.setFilename( "filenumber1" );
	document.setAuthor( "subs" );
	
	document.setApprover( tomuser );
	document.setData( "testfile data with really nothing." );
	
	assertTrue( ddp.add( document, "filenumber1", "subs" ) );
	
	assertTrue( ddp.delete( "filenumber1" ) );
	assertTrue( ddp.delete( "falsefile" ) ); // this doesn't fail because sql can handle the case of it not being
    }

    @Test
    public void testGetting()
	throws java.rmi.RemoteException
    {
	DocumentDataProcessor ddp = new DocumentDataProcessor();
	Document document = new Document();
	Document document2;
	document.setFilename( "filenumber1" );
	document.setAuthor( "subs" );
	
	document.setApprover( tomuser );
	document.setData( "testfile data with really nothing." );
	
	assertTrue( ddp.add( document, "filenumber1", "subs" ) );


	document2 = ddp.get( "filenumber1" );
	
	assertNotNull( "2 is null", document2 );

	assertTrue( ( document2.getFilename().compareTo( "filenumber1" ) == 0 ) );
	assertTrue( ( document2.getData().compareTo( "testfile data with really nothing." ) == 0 ) );


	assertTrue( ddp.delete( "filenumber1" ) );	
    }

    @Test
    public void testUpdateRoles()
	throws java.rmi.RemoteException
    {
	DocumentDataProcessor ddp = new DocumentDataProcessor();
	Document document = new Document();
	Document document2;
	document.setFilename( "filenumber1" );
	document.setAuthor( "subs" );
	
	document.setApprover( tomuser );
	document.setData( "testfile data with really nothing." );
	
	assertTrue( ddp.add( document, "filenumber1", "subs" ) );

	Role approver = new Role( true, false, false );
	Role reviewer = new Role( false, true, false );

	assertTrue( ddp.update( "filenumber1", laurauser, approver, true ) );
	document2 = ddp.get( "filenumber1" );
	assertNotNull("document2 is null", document2 );
	assertTrue( ( document2.getApprover().getUsername().compareTo("laura") == 0 ) );

	assertTrue( ddp.update( "filenumber1", tomuser, reviewer, true ) );
	assertTrue( ddp.update( "filenumber1", blakeuser, reviewer, true ) );
	assertTrue( ddp.update( "filenumber1", johnuser, reviewer, true ) );

	document2 = ddp.get( "filenumber1" );
	assertNotNull("document2 is null", document2 );
	assertTrue( document2.getReviewer().length == 3 );

	assertTrue( ddp.delete( "filenumber1" ) );		
    }


    @Test
    public void testUpdateComments()
	throws java.rmi.RemoteException
    {
	DocumentDataProcessor ddp = new DocumentDataProcessor();
	Document document = new Document();
	Document document2;
	document.setFilename( "filenumber1" );
	document.setAuthor( "subs" );
	
	document.setApprover( tomuser );
	document.setData( "testfile data with really nothing." );
	
	assertTrue( ddp.add( document, "filenumber1", "subs" ) );

	Comment comment1 = new Comment("Reword section 2", "tom");
	Comment comment2 = new Comment("hello, world", "blake");

	assertTrue( ddp.update( "filenumber1", comment1, true ) );
	assertTrue( ddp.update( "filenumber1", comment2, true ) );
       
	document2 = ddp.get( "filenumber1" );
	assertNotNull("document2 is null", document2 );
	assertTrue( document2.getComments().length == 2 );

	assertTrue( ddp.delete( "filenumber1" ) );		
    }
    
    @Test
    public void testUpdateGrade()
	throws java.rmi.RemoteException
    {
	DocumentDataProcessor ddp = new DocumentDataProcessor();
	Document document = new Document();
	Document document2;
	document.setFilename( "filenumber1" );
	document.setAuthor( "subs" );
	
	document.setApprover( tomuser );
	document.setData( "testfile data with really nothing." );
	
	assertTrue( ddp.add( document, "filenumber1", "subs" ) );

	assertTrue( ddp.update( "filenumber1", 50 ) );
       
	document2 = ddp.get( "filenumber1" );
	assertNotNull("document2 is null", document2 );
	assertTrue( document2.getGrade() == 50 );

	assertTrue( ddp.update( "filenumber1", 75 ) );
       
	document2 = ddp.get( "filenumber1" );
	assertNotNull("document2 is null", document2 );
	assertTrue( document2.getGrade() == 75 );

	assertTrue( ddp.update( "filenumber1", 25 ) );
       
	document2 = ddp.get( "filenumber1" );
	assertNotNull("document2 is null", document2 );
	assertTrue( document2.getGrade() == 25 );


	assertTrue( ddp.delete( "filenumber1" ) );
    }

    @Test
    public void testUpdatePass()
	throws java.rmi.RemoteException
    {
	DocumentDataProcessor ddp = new DocumentDataProcessor();
	Document document = new Document();
	Document document2;
	document.setFilename( "filenumber1" );
	document.setAuthor( "subs" );
	
	document.setApprover( tomuser );
	document.setData( "testfile data with really nothing." );
	
	assertTrue( ddp.add( document, "filenumber1", "subs" ) );

	assertTrue( ddp.update( "filenumber1", 25 ) );
       
	document2 = ddp.get( "filenumber1" );
	assertNotNull("document2 is null", document2 );
	assertTrue( document2.getGrade() == 25 );

	assertTrue( ddp.update("filenumber1", false ) );

	document2 = ddp.get( "filenumber1" );
	assertNotNull("document2 is null", document2 );
	assertTrue( document2.getGrade() == 0 );

	assertTrue( ddp.update("filenumber1", true ) );

	document2 = ddp.get( "filenumber1" );
	assertNotNull("document2 is null", document2 );
	assertTrue( document2.getGrade() == 100 );


	assertTrue( ddp.delete( "filenumber1" ) );
    }
}